//
//  VideoListViewController.m
//  LOL_PalmarHelper
//
//  Created by dlios on 15-6-20.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "VideoListViewController.h"

@interface VideoListViewController ()





@end

@implementation VideoListViewController
- (void)dealloc
{
    [_tableV release];
    [_tag release];
    [_array release];
    [_modelData release];
    [_mycell release];
    [_footer release];
    [_VPlayer release];
    [_heroname release];
    [_hud release];
    [_bu release];
    [_myvideoController release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.ismyplay = 1;
    
    self.navigationItem.title = self.heroname;
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(buA)];

    
    self.array = [NSMutableArray array];
    
    self.addPage = 1;
    
    [self getData];
    

    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    

//    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
   

    [self.view addSubview:self.tableV];
    
    self.tableV.delegate = self;
    
    self.tableV.dataSource = self;
    
    //   取消cell线
    
        self.tableV.separatorStyle = UITableViewCellEditingStyleNone;
    
////   添加上拉刷新
//
    
//    self.tableV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [self loadMoreData];
//    }];
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    
    self.tableV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
         [_tableV release];
    
    
    
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    [self.view addSubview:self.hud];
    
    self.hud.labelText = @"HHZ小组正卖力加载中";
    
    [self.hud show:YES];
    
    [_hud release];
    
    
    
    self.bu = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bu setTitle:@"重新加载" forState:UIControlStateNormal];
    self.bu.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.bu.layer.cornerRadius = 5;
    self.bu.frame = CGRectMake(0, 0, 80, 30);
    [self.view addSubview:self.bu];
    [self.bu addTarget:self action:@selector(buB) forControlEvents:UIControlEventTouchUpInside];
    self.bu.center = self.view.center;
    self.bu.alpha = 0.5;
    self.bu.hidden = YES;
    
    
    
    
    
    

}


- (void)buA
{
    
    [self.navigationController popViewControllerAnimated:YES];
   
}


- (void)loadMoreData
{
    

    
    self.addPage ++;
    [self getData];
    
}





-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableV reloadData];
}



- (void)getData
{

    
    [[HeroBaseHandle ShareHeroHeroDate] openDB];
    
    if (1 == self.addPage) {
        NSArray *arr = [[HeroBaseHandle ShareHeroHeroDate] selectAllmyVideoList];
        
        for (VideoListModel *list in arr) {
            
            if ([self.tag isEqualToString:list.tag]) {
                
                [self.array addObject:list];
                
            }
            
        }
        
    }
    
    
    
    NSString *str = [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?src=duowan&action=l&sk=&pageUrl=&heroEnName=&tag=%@&p=%ld", self.tag, (long)self.addPage];

     [AFN AFNGetData:str andBlock:^(id respondes) {
        
         
        
         if (![respondes isKindOfClass:[@"error" class]]) {
             
             if (self.addPage == 1) {
                 
                 [[HeroBaseHandle ShareHeroHeroDate] dropmyVideoListTable:self.tag];
                 
                 [[HeroBaseHandle ShareHeroHeroDate] createmyVideoListTable];
                 
                 [self.array removeAllObjects];

                 
             }
             
           
             
             NSMutableArray *arr = respondes;
             
             for (NSDictionary *dic in arr) {
                 
                 self.modelData = [[VideoListModel alloc] init];
                 
                 [self.modelData setValuesForKeysWithDictionary:dic];
                 
                 self.modelData.tag = self.tag;
                                  
                 [self.array addObject:self.modelData];
                 
                 if (self.addPage == 1) {
                      [[HeroBaseHandle ShareHeroHeroDate] insertmyVideoList:self.modelData];
                 }
                 
                 [_modelData release];
             }
             
             [self.tableV reloadData];
             [self.hud hide:YES];
             [self.tableV.footer endRefreshing];
             
             self.bu.hidden = YES;
             [self.bu removeFromSuperview];

         }else if ([respondes isKindOfClass:[@"error" class]]) {
             
             [self.tableV.footer endRefreshing];
             
             [self.hud hide:YES];
             UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
             [self.view addSubview:al];
             [al show];
             [al release];
             
             self.bu.hidden = NO;
             
             
         }

    }];
   
 
}


- (void)buB
{
    
    [self.hud show:YES];
    [self getData];
    self.bu.hidden = YES;
    
}

// 点击cell触发方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VideoListModel *mm = [self.array objectAtIndex:indexPath.row];
    
      self.myvid = mm.vid;
  
    [self playerGetData];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.array.count;
}






-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *str = @"mycell";
    self.mycell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (self.mycell == nil) {
        self.mycell = [[VideoListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        
    }
    

    
//    self.mycell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//    [UIView animateWithDuration:0.5 animations:^{
//        
//        self.mycell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//        
//        
//    }];

    
    self.mycell.videoDelegate = self;
        
    self.mycell.data = [self.array objectAtIndex:indexPath.row];
    self.mycell.myIndexpath = indexPath;
   
    return self.mycell;
}




-(void)shareVideo:(NSString *)titel andIndexpath:(NSIndexPath *)myIndexpath
{

    VideoListModel *mm = [self.array objectAtIndex:myIndexpath.row];
    
    self.myvid = mm.vid;

     NSString *str = [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=f&vid=%@&vu=null&sign=signxxxxx", self.myvid];
    
    [AFN AFNGetData:str andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            NSDictionary *dic = respondes;
            
            NSDictionary *dic1 = [dic objectForKey:@"result"];
            
            if ([[dic1 objectForKey:@"items"] isKindOfClass:[NSDictionary class]]) {
                
                NSDictionary *dic2 = [dic1 objectForKey:@"items"];
                
                
                NSDictionary *dic3 = [dic2 objectForKey:@"350"];
                
                NSDictionary *dic4 = [dic3 objectForKey:@"transcode"];
                
                NSArray *arr = [dic4 objectForKey:@"urls"];
                
                NSString *str1 = [arr objectAtIndex:0];
                
                self.shareUrl = str1;
            }
    
            }
        }];
    
    
    
    
    NSString *share = [NSString stringWithFormat:@"%@%@",titel,self.shareUrl];
    
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"559a76b067e58e5d91002844"
                                      shareText:share
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];

}







// 设置cell高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
}




- (void)playerGetData
{
    
    
    NSString *str = [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=f&vid=%@&vu=null&sign=signxxxxx", self.myvid];
    
    
    [AFN AFNGetData:str andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            NSDictionary *dic = respondes;
            
            NSDictionary *dic1 = [dic objectForKey:@"result"];
            
            if ([[dic1 objectForKey:@"items"] isKindOfClass:[NSDictionary class]]) {
                
                NSDictionary *dic2 = [dic1 objectForKey:@"items"];
                
                
                NSDictionary *dic3 = [dic2 objectForKey:@"350"];
                
                NSDictionary *dic4 = [dic3 objectForKey:@"transcode"];
                
                NSArray *arr = [dic4 objectForKey:@"urls"];
                
                NSString *str1 = [arr objectAtIndex:0];
                
                NSURL *url = [NSURL URLWithString:str1];
                
                self.myvideoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
                
//                [self.myvideoController.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];// 缩放方式为方位缩放
//                
//                [self.myvideoController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen]; //控制方式为满屏
                
                [self.myvideoController.moviePlayer prepareToPlay];
                
                [self.view addSubview:self.myvideoController.view];
                
                [self presentMoviePlayerViewControllerAnimated:self.myvideoController];
                
                [self.myvideoController.moviePlayer play];
                
                
                
                              
                
                
                
                
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buB1) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
                
    
                
                
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buB2) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
                
            }
            
            else
            {
                
                UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无此视频资源" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [self.view addSubview:al];
                [al show];
                [al release];
                
                [self dismissMoviePlayerViewControllerAnimated];
                
            }
            
            
            
            
        }else if ([respondes isKindOfClass:[@"error" class]]) {
            
            
            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            [self dismissMoviePlayerViewControllerAnimated];
        }
        
        
        
    }];
    
    
    
}


//  加载完走

- (void)buB2
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    
//    if (self.ismyplay == 2) {
//        
//            [self.myvideoController.moviePlayer pause];
//        [self.myvideoController dismissMoviePlayerViewControllerAnimated];
//        
//        NSLog(@"=======未加载完22=======");
//        
//        self.ismyplay = 1;
//    }
//  
//    else if (self.ismyplay == 1) {
//        
//         self.ismyplay = 3;
//        
//    NSLog(@"=======加载完22=======");
//
//    }
//    
    
  
}

// 点击done或播放完走

- (void)buB1
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
//    if (self.ismyplay == 1) {
//        
//              self.ismyplay = 2;
//        
//         NSLog(@"=======未加载完11=======");
//    }
//    
//    
//   else if (self.ismyplay == 3) {
//        
//        self.ismyplay = 1;
//        
//         NSLog(@"=======加载完11=======");
//        
//    }
    
  
    [self.myvideoController.view removeFromSuperview];
    
    [_myvideoController release];
    
    self.myvideoController = nil;
    
    
    
    
    
   
}





























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
       
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
